package com.bw.shopping.mvvm.viewmodel;

import android.annotation.SuppressLint;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;

import androidx.databinding.ObservableField;

import com.bw.greendao.dao.DaoSessionUtils;
import com.bw.greendao.entity.UserEntity;
import com.bw.mvvm_common.entity.BaseEntity;
import com.bw.mvvm_common.filed.UIField;
import com.bw.mvvm_common.mvvm.viewmodel.BaseViewModel;
import com.bw.mvvm_common.utils.IntentAction;
import com.bw.shopping.R;
import com.bw.shopping.adapter.OrderListAdapter;
import com.bw.shopping.entity.OrderListEntity;
import com.bw.shopping.entity.PaySignEntity;
import com.bw.shopping.entity.SubmitOrderEntity;
import com.bw.shopping.mvvm.model.OrderListModel;
import com.bw.shopping.mvvm.view.AdressActivity;
import com.bw.shopping.mvvm.view.PayActivity;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;

/**
 * @author KK
 * @description:
 * @date :2021/10/28 18:45
 */
public class OrderListViewModel extends BaseViewModel<OrderListModel> {

    public OrderListAdapter adapter = new OrderListAdapter();
    public ObservableField<String> name = new ObservableField<>("选择收货人");
    public ObservableField<String> adress = new ObservableField<>();
    public ObservableField<String> price = new ObservableField<>("0.00");
    public String mobile;
    List<OrderListEntity.DataBean.OrderGoodsListBean> orderGoodsList;
    OrderListEntity orderListEntity;
    int id;
    @Override
    public void oncreate() {
        super.oncreate();
        EventBus.getDefault().register(this);

    }

    @Subscribe(sticky = true)
    public void getdata(IntentAction intentAction){

        if(intentAction.ation.equals("sendOrder")){
            HashMap<String, Object> map = new HashMap<>();
            map.put("orderId",intentAction.num);
            m.requestOrderList(map)
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribeOn(Schedulers.io())
                    .subscribe(this);
            price.set(intentAction.money);
        }else if(intentAction.ation.equals("sendAdress")){
            name.set(intentAction.username);
            adress.set(intentAction.adress);
            mobile = intentAction.mobile;
            id = intentAction.id;
        }

    }

    @Override
    public void onDestory() {
        super.onDestory();
        EventBus.getDefault().unregister(this);
    }

    //跳转到地址列表页面
    public void adressListClick(){
        HashMap<String, Object> map = new HashMap<>();
        map.put(UIField.PAGECLAZZ, AdressActivity.class);
        startPage(map);
    }

    public void back(){
        finishPage();
    }

    /**
     * {
     *   "order": {
     *     "id": 0,
     *     "orderGoodsList": [
     *       {
     *         "goods_count": 0,
     *         "goods_desc": "string",
     *         "goods_icon": "string",
     *         "goods_id": 0,
     *         "goods_price": "string",
     *         "goods_sku": "string",
     *         "id": 0,
     *         "order_id": 0
     *       }
     *     ],
     *     "orderStatus": 0,
     *     "payType": 0,
     *     "shipAddress": {
     *       "id": 0,
     *       "ship_address": "string",
     *       "ship_is_default": 0,
     *       "ship_user_mobile": "string",
     *       "ship_user_name": "string",
     *       "user_id": 0
     *     },
     *     "totalPrice": "string"
     *   },
     *   "userId": 0
     * }
     */

    //提交订单
    public void submitOrderList(){
        //map
        HashMap<String, Object> map = new HashMap<>();

        //OrderMap
        HashMap<String, Object> orderMap = new HashMap<>();

        //user数据库
        List<UserEntity> userList = DaoSessionUtils.getInstance().getSession().queryBuilder(UserEntity.class).list();

        orderMap.put("id",orderListEntity.getData().getId());
        orderMap.put("orderGoodsList",orderGoodsList);
        orderMap.put("orderStatus",orderListEntity.getData().getOrderStatus());
        orderMap.put("payType",orderListEntity.getData().getPayType());

        HashMap<String, Object> shipAddress = new HashMap<>();
        shipAddress.put("id",id);
        shipAddress.put("ship_address",adress.get());
        shipAddress.put("ship_is_default",1);
        shipAddress.put("ship_user_mobile",mobile);
        shipAddress.put("ship_user_name",name.get());
        shipAddress.put("user_id",userList.get(0).getUser_id());

        orderMap.put("shipAddress",shipAddress);
        orderMap.put("totalPrice",Float.valueOf(price.get()));


        map.put("order",orderMap);
        map.put("userId",userList.get(0).getUser_id());
        m.requestSubmitOrder(map)
                .observeOn(AndroidSchedulers.mainThread())
                .subscribeOn(Schedulers.io())
                .subscribe(this);
    }
    @Override
    protected void result(BaseEntity baseEntity) {
        if(baseEntity instanceof OrderListEntity){

            orderListEntity = (OrderListEntity) baseEntity;
            orderGoodsList = orderListEntity.getData().getOrderGoodsList();
            adapter.setNewInstance(orderGoodsList);
        }
        if(baseEntity instanceof SubmitOrderEntity){
            showMsg("提交订单成功");
            HashMap<String, Object> hashMap = new HashMap<>();
            hashMap.put("orderId",orderListEntity.getData().getId());
            hashMap.put("totalPrice",Integer.valueOf(orderListEntity.getData().getTotalPrice()));
            m.requestPaySign(hashMap)
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribeOn(Schedulers.io())
                    .subscribe(this);

        }
        if(baseEntity instanceof PaySignEntity){
            PaySignEntity paySignEntity = (PaySignEntity) baseEntity;
            showMsg("获取支付宝sign成功");
            HashMap<String, Object> hashMap = new HashMap<>();
            Bundle bundle = new Bundle();
            bundle.putString("paysign",paySignEntity.getData());
            hashMap.put(UIField.PAGEBUNDLE,bundle);
            hashMap.put(UIField.PAGECLAZZ,PayActivity.class);
            startPage(hashMap);
        }

        if(baseEntity.getMessage().equals("订单支付成功")){
            showMsg("订单支付成功");
            finishPage();
        }


    }


    //支付成功
    @Subscribe(sticky = true)
    public void isPay(IntentAction intentAction){
        if(intentAction.ation.equals("isPay")){
            HashMap<String, Object> map = new HashMap<>();
            map.put("orderId",orderListEntity.getData().getId());
            m.requestPayment(map)
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribeOn(Schedulers.io())
                    .subscribe(this);

        }

    }



}
